import { Redirect, Route, RouteProps, useLocation } from 'react-router-dom'
import { hasToken } from '@/utils/storage'

interface Props extends RouteProps {
    component: React.ComponentType<any>
}

export default function AuthRoute({ component: Component, ...rest }: Props) {
    const location = useLocation()
    // console.log(location)
    return (
      <Route
          { ...rest } 
          render={() => {
              if (hasToken()) {
                  return <Component></Component>
              } else {
                  return <Redirect to={{
                    pathname: '/login',
                    state: {
                            // 从哪来的
                            from: location.pathname
                        }
                    }}
                ></Redirect> 
              }
          }}
      ></Route>
    )
}
